package com.edu.whty.common.tools;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class FileUtils {

	private static final Logger LOGGER = LogManager.getLogger(FileUtils.class);

	private static Lock lock = new ReentrantLock();

	public static void createFile(String filePath) {
		lock.lock();
		try {
			LOGGER.info("创建文件=>" + filePath);
			File file = new File(filePath);
			if (!file.exists()) {
				File parent = file.getParentFile();
				if (!parent.exists()) {
					parent.mkdirs();
				}
				file.createNewFile();
			}
		} catch (IOException ex) {
			LOGGER.error(ex.getMessage(), ex);
		} finally {
			lock.unlock();
		}
	}

	public static String toStringSize(Float size) {
		DecimalFormat df = new DecimalFormat("#.##");
		String mysize = "";
		if (size >= 1024f * 1024f * 1024f * 1024f) {
			mysize = df.format(size / 1024f / 1024f / 1024f / 1024f) + "TB";
		} else if (size >= 1024 * 1024 * 1024) {
			mysize = df.format(size / 1024f / 1024f / 1024f) + "GB";
		} else if (size >= 1024 * 1024) {
			mysize = df.format(size / 1024f / 1024f) + "MB";
		} else if (size >= 1024) {
			mysize = df.format(size / 1024f) + "KB";
		} else {
			mysize = size + "B";
		}
		return mysize;
	}

	public static String getFileMD5(File file) {
		if (!file.isFile()) {
			return null;
		}
		MessageDigest digest = null;
		FileInputStream in = null;
		byte[] buffer = new byte[10240];
		try {
			digest = MessageDigest.getInstance("MD5");
			in = new FileInputStream(file);
			int len;
			while ((len = in.read(buffer)) != -1) {
				digest.update(buffer, 0, len);
			}
			in.close();
		} catch (Exception ex) {
			LOGGER.error("获取文件MD5错误", ex);
			return null;
		} finally {
			try {
				if (in != null)
					in.close();
			} catch (IOException ex) {
				LOGGER.error("关闭输入输出流异常", ex);
			}
		}
		BigInteger bigInt = new BigInteger(1, digest.digest());
		String result = bigInt.toString(16);
		int length = result.length();
		if (length < 32) {
			for (int i = 0; i < 32 - length; i++) {
				result = "0" + result;
			}
		}
		return result;
	}
	
	public static void removeDir(File dir) {
		if (dir == null || !dir.exists()) {
			LOGGER.error("目录不存在...");
			return;
		}
		
		if (!dir.isDirectory()) {
			LOGGER.error(dir.getAbsolutePath() + " 不是目录");
			return;
		}
		
		recurRemoveDir(dir);
	}
	
	private static void recurRemoveDir(File dir) {
		File[] files = dir.listFiles();
		if (files != null && files.length > 0) {
			for (File file : files) {
				if (file.isFile()) {
					file.delete();
				} else {
					recurRemoveDir(file);
				}
			}
		}
		dir.delete();
	}
	public static String size2string(long size) {
		DecimalFormat df = new DecimalFormat("#.##");
		String mysize = "";
		if (size > 1024 * 1024 * 1024) {
			mysize = df.format(size / 1024f / 1024f / 1024f) + "GB";
		} else if (size > 1024 * 1024) {
			mysize = df.format(size / 1024f / 1024f) + "MB";
		} else if (size > 1024) {
			mysize = df.format(size / 1024f) + "KB";
		} else {
			mysize = size + "B";
		}
		return mysize;
	}
}
